package lang;

import org.junit.Assert;
import org.junit.Test;

/**
 * @Author tuyou_cd
 * @Date 2020/8/4 12:22
 */
public class StringTest {

    /**
     * 常量池测试
     */
    @Test
    public void poolTest() {

        String s1 = "123";
        String s2 = "123";
        Assert.assertTrue(s1 == s2);

        s1 = "123";
        s2 = new String("123");
        Assert.assertFalse(s1 == s2);
        Assert.assertTrue(s1 == s2.intern()); //intern方法缓存到常量池里，并返回

        String s3 = new String("456");
        String s4 = s3.intern();
        String s5 = "456";
        Assert.assertTrue(s4 == s5);
        Assert.assertFalse(s3 == s5);
    }
}
